package com.ruoyi.project.lesson.uploadtask.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.lesson.uploadtask.domain.UploadTask;
import com.ruoyi.project.lesson.uploadtask.service.IUploadTaskService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 上传任务Controller
 *
 * @author shixiangwen
 * @date 2020-12-11
 */
@Controller
@RequestMapping("/lesson/uploadtask")
public class UploadTaskController extends BaseController {
    private String prefix = "lesson/uploadtask";

    @Autowired
    private IUploadTaskService uploadTaskService;

    @RequiresPermissions("lesson:uploadtask:view")
    @GetMapping()
    public String uploadtask() {
        return prefix + "/uploadtask";
    }

    /**
     * 查询上传任务列表
     */
    @RequiresPermissions("lesson:uploadtask:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(UploadTask uploadTask) {
        startPage();
        List<UploadTask> list = uploadTaskService.selectUploadTaskList(uploadTask);
        return getDataTable(list);
    }

    /**
     * 导出上传任务列表
     */
    @RequiresPermissions("lesson:uploadtask:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(UploadTask uploadTask) {
        List<UploadTask> list = uploadTaskService.selectUploadTaskList(uploadTask);
        ExcelUtil<UploadTask> util = new ExcelUtil<UploadTask>(UploadTask.class);
        return util.exportExcel(list, "uploadtask");
    }

    /**
     * 新增上传任务
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存上传任务
     */
    @RequiresPermissions("lesson:uploadtask:add")
    @Log(title = "上传任务", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(UploadTask uploadTask) {
        return toAjax(uploadTaskService.insertUploadTask(uploadTask));
    }

    /**
     * 修改上传任务
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        UploadTask uploadTask = uploadTaskService.selectUploadTaskById(id);
        mmap.put("uploadTask", uploadTask);
        return prefix + "/edit";
    }

    /**
     * 修改保存上传任务
     */
    @RequiresPermissions("lesson:uploadtask:edit")
    @Log(title = "上传任务", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(UploadTask uploadTask) {
        return toAjax(uploadTaskService.updateUploadTask(uploadTask));
    }

    /**
     * 删除上传任务
     */
    @RequiresPermissions("lesson:uploadtask:remove")
    @Log(title = "上传任务", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(uploadTaskService.deleteUploadTaskByIds(ids));
    }

    @RequiresPermissions("lesson:uploadtask:edit")
    @Log(title = "转码视频", businessType = BusinessType.UPDATE)
    @PostMapping("/transcode")
    @ResponseBody
    public AjaxResult transcode(Long id, Integer reTranscode) {
        reTranscode = reTranscode == null ? 0 : reTranscode;
        return toAjax(uploadTaskService.transcode(id, reTranscode));
    }

}
